﻿using System;
using System.Collections.Generic;
using System.Linq;
using Porownywarka.Interfejsy;

namespace Porownywarka.Sortowanie
{
    public class CountingSort<T> : ISortMethod<T>
        where T : IComparable<T> 
    {
        public T[] SortedData { get; private set; }

        public T[] Sort(T[] tablica, object l)
        {
            T[] tab2 = (T[])tablica.Clone();
            return Sortuj(tab2);
        }

        private T[] Sortuj(T[] tab)
        {
            /*uint min = tab[0];
            uint max = tab[0];

            uint[] res = new uint[tab.Length];

            //< 0 wczesniejsza this
            for (int i = 0; i < tab.Length; i++)
            {
                if (tab[i].CompareTo(max) > 0)
                    max = tab[i];

                if (tab[i].CompareTo(min) < 0)
                    min = tab[i];
            }

            uint[] cData = new uint[(max - min) + 1];

            for (int i = 0; i < tab.Length; i++)
                cData[tab[i] - min] += 1;

            for (int i = 1; i < cData.Length; i++)
                cData[i] += cData[i - 1];

            for (int i = tab.Length-1; i >= 0 ; i--)
            {
                res[cData[tab[i] - min] - 1] = tab[i];
                cData[tab[i] - min]--;
            }

            return res;*/
            return new T[0];
        }

        /*public T[] Sort(T[] tablica)
        {
            T min = tablica[0];
            T max = tablica[0];

            uint t = Convert.ToUInt32(tablica[0]);

            uint[] res = new uint[tablica.Length];

            //< 0 wczesniejsza this
            for (int i = 0; i < tablica.Length; i++)
            {
                if (tablica[i].CompareTo(max) > 0)
                    max = tablica[i];

                if (tablica[i].CompareTo(min) < 0)
                    min = tablica[i];
            }

            uint[] cData = new uint[(max - min) + 1];

            for (int i = 0; i < tab.Length; i++)
                cData[tab[i] - min] += 1;

            for (int i = 1; i < cData.Length; i++)
                cData[i] += cData[i - 1];

            for (int i = tab.Length - 1; i >= 0; i--)
            {
                res[cData[tab[i] - min] - 1] = tab[i];
                cData[tab[i] - min]--;
            }

            return res;
        }*/
    }
}
